﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Castle.ActiveRecord;
using NHibernate.Criterion;
using LIB;

public partial class admin_system_editor : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                using (new SessionScope())
                {
                    int catID = ConvertUtility.ToInt32(Request.QueryString["catID"]);
                    Category_Info catInfo = Category_Info.Find(catID);
                    if (catID > 0)
                    {
                        hidCatID.Value = catID.ToString();
                        int id = ConvertUtility.ToInt32(Request.QueryString["id"]);
                        if (id > 0)
                        {
                            Category_Info cat = Category_Info.Find(id);
                            hidID.Value = id.ToString();
                            initForm(true, cat.catName);
                        }
                        else
                        {
                            initForm(false, catInfo.catName);
                        }
                    }
                }

                fail.Visible = false;
            }
        }
        catch (Exception ex) { }
    }
    protected void initForm(bool isEdit, string catName)
    {
        if (isEdit)
        {
            btnAdd.Visible = false;
            btnEdit.Visible = true;
            litPos.Text = "<a href='/admin/system/default.aspx'>Hệ thống</a> >> Sửa " + catName;
            fillData();
        }
        else
        {
            btnAdd.Visible = true;
            btnEdit.Visible = false;
            litPos.Text = "<a href='/admin/system/default.aspx'>Hệ thống</a> >> Thêm mới " + catName;
        }
    }

    protected void fillData()
    {
        try
        {
            Category_Info catInfo = Category_Info.Find(ConvertUtility.ToInt32(hidID.Value));
            txtCatName.Text = catInfo.catName;
            imgAvatar.ImageUrl = App_Constant.PATH_UP_IMAGE + "/thumb/" + catInfo.catAvatar;
            hidAvatar.Value = catInfo.catAvatar;
            txtCatOrder.Text = ConvertUtility.ToString(catInfo.catOrder);
            txtDes.Text = catInfo.catDes;
            txtMetaKey.Text = catInfo.catMetaKey;
            txtMetaDes.Text = catInfo.catMetaDes;
            txtMetaTitle.Text = catInfo.catMetaTitle;
            if (catInfo.isHomePage)
                chkIsHomepage.Checked = true;
            else
                chkIsHomepage.Checked = false;
        }
        catch (Exception ex) { }
    }
    protected Category_Info getCatInfo(Category_Info catInfo)
    {
        catInfo.catParentID = Category_Info.Find(ConvertUtility.ToInt32(hidCatID.Value));
        catInfo.catName = StringUtility.RemoveInvalidHTMLTags(txtCatName.Text, null);
        catInfo.catAvatar = fileUpAvatar.HasFile ? App_File.tienToFile() + "." + fileUpAvatar.FileName : hidAvatar.Value;
        catInfo.catOrder = ConvertUtility.ToInt32(txtCatOrder.Text);
        catInfo.catLang = ConvertUtility.ToInt32(Session["language"]);
        catInfo.catDes = StringUtility.RemoveInvalidHTMLTags(txtDes.Text, null);
        catInfo.catMetaDes = StringUtility.RemoveInvalidHTMLTags(txtMetaDes.Text, null);
        catInfo.catMetaKey = StringUtility.RemoveInvalidHTMLTags(txtMetaKey.Text, null);
        catInfo.catMetaTitle = StringUtility.RemoveInvalidHTMLTags(txtMetaTitle.Text, null);
        if (chkIsHomepage.Checked)
            catInfo.isHomePage = true;
        else
            catInfo.isHomePage = false;
        return catInfo;
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        try
        {
            Category_Info catInfo = getCatInfo(new Category_Info());
            if (fileUpAvatar.HasFile)
                App_File.uploadFileOnSerVer(fileUpAvatar, App_Constant.PATH_UP_IMAGE + "/", 150, 100);
            using (new SessionScope())
                catInfo.Create();
            Response.Redirect("/admin/system/default.aspx?id=" + ConvertUtility.ToInt32(hidCatID.Value));
        }
        catch (Exception ex)
        {
            fail.Visible = true;
        }
    }
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        try
        {
            Category_Info catInfo = getCatInfo(Category_Info.Find(ConvertUtility.ToInt32(hidID.Value)));
            if (fileUpAvatar.HasFile)
                App_File.uploadFileOnSerVer(fileUpAvatar, App_Constant.PATH_UP_IMAGE + "/", 150, 100);
            using (new SessionScope())
                catInfo.SaveAndFlush();
            Response.Redirect("/admin/system/default.aspx?id=" + ConvertUtility.ToInt32(hidCatID.Value));
        }
        catch (Exception ex)
        {
            fail.Visible = true;
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        if (hidID.Value != "")
            fillData();
        else
            ControlUtility.EmptyTextBoxes(this);

    }
}